package com.wyd.resourcetj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.wyd.resourcetj.dao.IAdminstratorDao;
import com.wyd.resourcetj.model.Adminstrator;
import com.wyd.resourcetj.service.IAdminstratorService;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * AdminstratorController
 */
@RestController
@RequestMapping(path="/adminstrator",method = RequestMethod.GET)
public class AdminstratorController {

    @Resource
    private IAdminstratorDao adminstratorDao;





    @ApiOperation(value = "查询所有")

    @GetMapping(value = "/find")
        public Map findAll(){
 
         List<Adminstrator> list = adminstratorDao.findAll();
     
     Map<String,Object> map = new HashMap<String, Object>();
     map.put("code", 0);
     map.put("msg", "查询成功");
     map.put("count", list.size());
     map.put("data", list);
     
     
     
     return map;
 
        }

        



    @ApiOperation(value = "添加管理员",notes="添加管理员")
	  
    @PostMapping(value = "addOredit")
      
      public Adminstrator add(@RequestBody Adminstrator a) {
          
          return adminstratorDao.save(a);
          
      }

      @ApiOperation(value="根据账号修改密码",notes = "根据账号修改密码")
      @ApiImplicitParams({
          @ApiImplicitParam(name="adminName",value ="账号",required=true,paramType ="path"),
          @ApiImplicitParam(name="password",value ="密码",required=true,paramType ="path")
          
      })
      @PutMapping(value = "/{adminName}/{password}")
      public int editAdminstrator(@PathVariable String adminName,@PathVariable String password) {
          
          return adminstratorDao.updateAdminstrator(adminName,password);
          
      }



      @ApiOperation(value = "查询所有管理员",notes = "查询所有管理员")
      @PostMapping(value ="/findAll")
      public List<Adminstrator> findAllAdminstrator(){
          
         
              List<Adminstrator> list=new ArrayList<Adminstrator>();

              list=adminstratorDao.findAll();


          return list;
      }
      @ApiOperation(value = "根据编号删除管理员",notes="根据编号删除管理员")
      @ApiImplicitParam(name="id",value ="资源编号",required=true,paramType ="path")
      @DeleteMapping(value ="/{id}")
        public void del(@PathVariable Integer id) {
            
          adminstratorDao.deleteById(id);
             
            
        } 


      /* @ApiOperation(value = "根据编号删除管理员",notes="根据编号删除管理员")
        @ApiImplicitParams({
         @ApiImplicitParam(name="n",value ="0 删除，1 恢复",required=true,paramType ="path"),
        @ApiImplicitParam(name="id",value ="资源编号",required=true,paramType ="path")
        })
        @PutMapping(value = "/del/{id}/{n}")
          public void delAdminstrator(@PathVariable Integer id,@PathVariable Integer n) {
              
           // System.out.println(id+","+n);
            adminstratorDao.deleteAdminstrator(id, n);
               
          
              
          } */

          @Resource
         private  IAdminstratorService adminstratorService;


          @ApiOperation(value = "根据编号删除管理员",notes="根据编号删除管理员")
          /* @ApiImplicitParams({
           @ApiImplicitParam(name="adm_id",value ="要删除管理员的编号",required=true,paramType ="path")
          }) */
          @GetMapping()
            public void delMoreAdminstrator(@RequestParam(value="adm_id") String adm_id) {
                
                System.out.println(adm_id);
              // 接收包含stuId的字符串，并将它分割成字符串数组
    String[] stuList = adm_id.split(",");
    System.out.println(stuList.toString());
    // 将字符串数组转为List<Intger> 类型
    List<Integer> LString = new ArrayList<Integer>();
    for(String str : stuList){
        LString.add(new Integer(str));
    }

              // 调用service层的批量删除函数
              adminstratorService.deleteMoreAdminstrator(LString);
    //return Msg.success().add("数组", LString);
                 
            
                
            }

          

            @ApiOperation(value = "登录",notes="登录")
            @ApiImplicitParams({
              @ApiImplicitParam(name="adminName",value ="账号",required=true,paramType ="path"),
              @ApiImplicitParam(name="password",value ="密码",required=true,paramType ="path")
             })
            @GetMapping(value = "/login1/{adminName}/{password}")
              
              public  int login(@PathVariable String adminName,@PathVariable String  password) {
                  

             List<Adminstrator> list=adminstratorDao.login(adminName, password);

                  return list.size();
                  
              }




}